package com.eastfair.pay.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PayBody {
    //消息ID
     String msgId;
    //报文请求时间，格式yyyy-MM-ddHH:mm:ss
    String requestTimestamp;
    //商户订单号
    String merOrderId;
    //请求系统预留字段
    String srcReserve;
    //商户号
    String mid;
    //终端号
    String tid;
    //业务类型
    String instMid;
    //分账标记
    String divisionFlag;
    //平台商户分账金额
    String platformAmount;
    //商品信息
    List<GoodsItem> goods;
    //子订单信息
    List<SubOrderItem> subOrders;
    //商户附加数据
    String attachedData;
    //账单描述
    String orderDesc;
    //商品标记
    String goodsTag;
    //订单原始金额
    String originalAmount;
    //支付总金额
    String totalAmount;
    //支付结果通知地址
    String notifyUrl;
    //网页跳转地址
    String returnUrl;
    //系统ID
    String systemId;
    //实名认证姓名
    String name;
    //实名认证手机号
    String mobile;
    //实名认证证件类型
    String certType;
    //实名认证证件号
    String certNo;
    //是否需要实名认证
    String fixBuyer;
    //是否需要限制信用卡支付
    String limitCreditCard;
    //担保交易标识
    String secureTransaction;
    /**
     * 二维码ID
     */
    String qrCodeId;
    /**
     * 业务日期
     */
    String billDate;
    /**
     * 业务单号（银联）
     */
    String billNo;
    /**
     * 交易类型
     */
    String tradeType;
    /**
     * 用户子标识
     */
    String subOpenId;
    /**
     * 微信子商户
     */
    String subAppId;
    /**
     * 卡号
     */
    String chnlType;
    /**
     * 卡号
     */
    String transType;

    String toJson() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        if (this.msgId != null) sb.append("\"msgId\":\"" + this.msgId + "\",");
        if (this.requestTimestamp != null) sb.append("\"requestTimestamp\":\"" + this.requestTimestamp + "\",");
        if (this.merOrderId != null) sb.append("\"merOrderId\":\"" + this.merOrderId + "\",");
        if (this.srcReserve != null) sb.append("\"srcReserve\":\"" + this.srcReserve + "\",");
        if (this.mid != null) sb.append("\"mid\":\"" + this.mid + "\",");
        if (this.tid != null) sb.append("\"tid\":\"" + this.tid + "\",");
        if (this.instMid != null) sb.append("\"instMid\":\"" + this.instMid + "\",");
        if (this.divisionFlag != null) sb.append("\"divisionFlag\":\"" + this.divisionFlag + "\",");
        if (this.platformAmount != null) sb.append("\"platformAmount\":\"" + this.platformAmount + "\",");
        if (this.goods != null && this.goods.size() > 0) {
            sb.append("\"goods\":[");
            for (int i = 0; i < goods.size(); i++) {
                sb.append(goods.get(i));
                sb.append(",");
            }
            if (sb.charAt(sb.length() - 1) == ',')
                sb.deleteCharAt(sb.length() - 1);
            sb.append("],");
        }
        if (this.subOrders != null && this.subOrders.size() > 0) {
            sb.append("\"subOrders\":[");
            for (int i = 0; i < subOrders.size(); i++) {
                sb.append(subOrders.get(i));
                sb.append(",");
            }
            if (sb.charAt(sb.length() - 1) == ',')
                sb.deleteCharAt(sb.length() - 1);
            sb.append("],");
        }
        if (this.totalAmount != null) sb.append("\"totalAmount\":\"" + this.totalAmount + "\",");
        if (this.orderDesc != null) sb.append("\"orderDesc\":\"" + this.orderDesc + "\",");
        if (this.originalAmount != null) sb.append("\"originalAmount\":\"" + this.originalAmount + "\",");
        if (this.goodsTag != null) sb.append("\"goodsTag\":\"" + this.goodsTag + "\",");
        if (this.attachedData != null) sb.append("\"attachedData\":\"" + this.attachedData + "\",");
        if (this.notifyUrl != null) sb.append("\"notifyUrl\":\"" + this.notifyUrl + "\",");
        if (this.returnUrl != null) sb.append("\"returnUrl\":\"" + this.returnUrl + "\",");
        if (this.systemId != null) sb.append("\"systemId\":\"" + this.systemId + "\",");
        if (this.name != null) sb.append("\"name\":\"" + this.name + "\",");
        if (this.certType != null) sb.append("\"certType\":\"" + this.certType + "\",");
        if (this.certNo != null) sb.append("\"certNo\":\"" + this.certNo + "\",");
        if (this.fixBuyer != null) sb.append("\"fixBuyer\":\"" + this.fixBuyer + "\",");
        if (this.secureTransaction != null) sb.append("\"secureTransaction\":\"" + this.secureTransaction + "\",");
        if (this.mobile != null) sb.append("\"mobile\":\"" + this.mobile + "\",");
        if (this.limitCreditCard != null) sb.append("\"limitCreditCard\":\"" + this.limitCreditCard + "\",");
        if (this.qrCodeId != null) sb.append("\"qrCodeId\":\"" + this.qrCodeId + "\",");
        if (this.billDate != null) sb.append("\"billDate\":\"" + this.billDate + "\",");
        if (this.billNo != null) sb.append("\"billNo\":\"" + this.billNo + "\",");
        if (this.tradeType != null) sb.append("\"tradeType\":\"" + this.tradeType + "\",");
        if (this.subOpenId != null) sb.append("\"subOpenId\":\"" + this.subOpenId + "\",");
        if (this.subAppId != null) sb.append("\"subAppId\":\"" + this.subAppId + "\",");
        if (this.chnlType != null) sb.append("\"chnlType\":\"" + this.chnlType + "\",");
        if (this.transType != null) sb.append("\"transType\":\"" + this.transType + "\",");
        if (sb.charAt(sb.length() - 1) == ',')
            sb.deleteCharAt(sb.length() - 1);
        sb.append("}");
        return sb.toString();
    }

    @Override
    public String toString() {
        return this.toJson();
    }

    static class GoodsItem {
        //商品ID
        String goodsId;
        //商品名称
        String goodsName;
        //商品数量
        String quantity;
        //商品单价（分）
        String price;
        //商品分类
        String goodsCategory;
        //商品说明
        String body;
        //子商户号
        String subMerchantId;
        //子商户商品总额
        int subOrderAmount;

        String toJson() {
            StringBuilder sb = new StringBuilder();
            sb.append("{");
            if (this.goodsId != null) sb.append("\"goodsId\":\"" + this.goodsId + "\",");
            if (this.goodsName != null) sb.append("\"goodsName\":\"" + this.goodsName + "\",");
            if (this.quantity != null) sb.append("\"quantity\":\"" + this.quantity + "\",");
            if (this.price != null) sb.append("\"price\":\"" + this.price + "\",");
            if (this.goodsCategory != null) sb.append("\"goodsCategory\":\"" + this.goodsCategory + "\",");
            if (this.body != null) sb.append("\"body\":\"" + this.body + "\",");
            if (this.subMerchantId != null) sb.append("\"subMerchantId\":\"" + this.subMerchantId + "\",");
            if (this.subOrderAmount != 0) sb.append("\"subOrderAmount\":\"" + this.subOrderAmount + "\",");
            if (sb.charAt(sb.length() - 1) == ',')
                sb.deleteCharAt(sb.length() - 1);
            sb.append("}");
            return sb.toString();
        }

        @Override
        public String toString() {
            return this.toJson();
        }
    }

    static class SubOrderItem {
        //子商户号
        String mid;
        //子商户分账金额
        int totalAmount;

        String toJson() {
            StringBuilder sb = new StringBuilder();
            sb.append("{");
            if (this.mid != null) {
                sb.append("\"mid\":\"" + this.mid + "\",");
            }
            if (this.totalAmount != 0) {
                sb.append("\"totalAmount\":\"" + this.totalAmount + "\",");
            }
            if (sb.charAt(sb.length() - 1) == ',')
                sb.deleteCharAt(sb.length() - 1);
            sb.append("}");
            return sb.toString();
        }

        @Override
        public String toString() {
            return this.toJson();
        }
    }

}